package com.lemon.smartsafety.utils.common;

import android.util.Log;

import com.lemon.smartsafety.utils.LogUtils;

import java.io.File;

/**
 * Created by Cooliean on 16/4/4.
 */
public class LemonFileHelper {
    public final static int TYPE_IMAGE = 0;
    public final static int TYPE_VOICE = 1;
    public final static int TYPE_VIDEO = 2;
    public final static int TYPE_OTHER = 3;
    public final static int TYPE_DOC   = 4;
    public final static int TYPE_PPT   = 5;
    public final static int TYPE_XLS   = 6;
    public final static int TYPE_PDF   = 7;

    //PHOTO(0), VOICE(1), VIDEO(2), OTHER(3);

    /**
     * 通过文件后缀判断文件类型
     *
     * @param url 文件文件地址
     * @return
     */
    public static int getFileType(String url) {
        if (url == null || url.length() == 0) {
            return TYPE_OTHER;
        }
        String ext = getFileExt(url);
        if (ext.toLowerCase().equals("jpg") || ext.toLowerCase().equals("bmp") || ext.toLowerCase().equals("png") || ext.toLowerCase().equals("jpeg") || ext.toLowerCase().equals("gif")) {
            return TYPE_IMAGE;
        } else if (ext.toLowerCase().equals("doc") || ext.toLowerCase().equals("docx")) {
            return TYPE_DOC;
        } else if (ext.toLowerCase().equals("xls") || ext.toLowerCase().equals("xlsx")) {
            return TYPE_XLS;
        } else if (ext.toLowerCase().equals("ppt") || ext.toLowerCase().equals("pptx")) {
            return TYPE_PPT;
        } else if (ext.toLowerCase().equals("pdf")) {
            return TYPE_PDF;
        } else if (ext.toLowerCase().equals("amr")) {
            return TYPE_VOICE;
        } else if (ext.toLowerCase().equals("mp4")) {
            return TYPE_VIDEO;
        }
        return TYPE_OTHER;
    }

    public static boolean isImageUrl(String url) {
        if (getFileType(url) == LemonFileHelper.TYPE_IMAGE) {
            return true;
        }
        return false;
    }

    public static boolean isVoiceUrl(String url) {
        if (getFileType(url) == LemonFileHelper.TYPE_VOICE) {
            return true;
        }
        return false;
    }

    public static boolean isVideoUrl(String url) {
        if (getFileType(url) == LemonFileHelper.TYPE_VIDEO) {
            return true;
        }
        return false;
    }

    /**
     * 获取文件后最
     *
     * @param fileUri
     * @return
     */
    public static String getFileExt(String fileUri) {
        File   file     = new File(fileUri);
        String fileName = file.getName();
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        return fileType;
    }
}
